/*
 * Script: messages.js
 *  My Fading message log
 * 
 * Author:
 * 	Jakub Miara (jakub.miara@gmail.com)
 * 
 * License:
 * 	MIT-style license.
 * 
 * Requires mootools v 1.0
 * 
 */
 
var MessageLog = new Class({
		
	initialize: function(divid,options){
		this.messages = [];	
		this.divid = divid;
		this.div =  $(divid);
		this.opts = new Options();
		this.opts.setOptions({messageClass : '', timeout:3000},options);
		this.divs = [];
		
	},
	
	realign:function(){
		var count = 0;
		var sumsize = 0;


		this.divs.every(function(el,idx){		
				count++;
				sumsize += el.getSize().size.y;
				return true;
		});
		
		this.divs.every(function(el,idx){ 
			el.targetPos = window.getScrollTop() + document.body.clientHeight  - sumsize;
			new Fx.Style(el,'top',{duration:300}).start(el.targetPos);
			new Fx.Style(el,'left',{duration:300}).start(Math.max(0,(window.getSize().size.x/2) - (el.getSize().size.x)/2));
			sumsize -= el.getSize().size.y;
			return true;
		});

	},
	
	log: function(message){
		var tdiv = document.createElement('div');
	
		tdiv.innerHTML=message;
		
		tdiv.placing = document.body;
		tdiv.style.position = 'absolute';
		tdiv.box = this;
		

		if(this.opts.options.messageClass){
			$(tdiv).addClass(this.opts.options.messageClass);
		}

		new Fx.Style(tdiv,'opacity').set(0);
			
		$(document.body).adopt(tdiv);
		this.divs.push(tdiv);
				
		new Fx.Style(tdiv,'top').set(window.getScrollTop() + document.body.clientHeight - tdiv.getSize().size.y);
		tdiv.targetPos = tdiv.getPosition().y;
	

		new Fx.Style(tdiv,'left').set(Math.max(0,(window.getSize().size.x/2) - (tdiv.getSize().size.x)/2));		
		new Fx.Style(tdiv,'opacity',{duration:1000}).start(1);		


		this.divs.every(function(el,idx){
				if(el != tdiv){
					el.targetPos = el.targetPos - tdiv.getSize().size.y;
					new Fx.Style(el,'top',{duration:300}).start(el.targetPos);
					return true;
				}				
		});

		var clClass = this.opts.options.closureClass;
		var allDivs = this.divs;
		if(clClass){
			$(tdiv).getChildren().every(function(el,idx){
				el.addEvent('click',function(){
				if(tdiv.removing){return;}else{tdiv.removing = true;}
				new Fx.Style(tdiv,'opacity',{duration:300}).start(0);
				var fx = new Fx.Style(tdiv,'height',{duration:600});
				fx.addEvent('onComplete',function(){
					var box = tdiv.box;
					if(tdiv.parentNode){
						allDivs.remove(tdiv);
						tdiv.remove();
					}
					box.realign();
					});
				fx.start(0);
				});
			return true;
			});
		}
		
		
		var fx = new Fx.Style(tdiv,'opacity',{duration:600});
		var fx2 = new Fx.Style(tdiv,'height',{duration:900});
		fx2.addEvent('onComplete',function(){
		if(tdiv.removing){return;}else{tdiv.removing = true;}
			allDivs.remove(tdiv);			
			tdiv.box.realign();
			tdiv.remove();
			});
		
		fx.start.delay(this.opts.options.timeout,fx,(0));
		fx2.start.delay(this.opts.options.timeout+500,fx2,(0));

	}
}); 